#!/bin/sh

# 注入环境变量
. $CI_SHELL_PATH/scripts/build-env-generate.sh

set +xe

# project 有些比较特殊，比如 panel contact-hook 这种，只能手动
project=$1

# qidian 项目才需要子目录
if [ $project = "contact-book" ] \
 || [ $project = "panel" ] \
 || [ $project = "user-profile" ]; then
  dist_path=./dist/$project
else
  dist_path=./dist
fi

# 注入 ROLLBACK_VERSION_ID 到结果目录，提供给 jenkins 使用
echo "$ROLLBACK_VERSION_ID" > ${dist_path}/ROLLBACK_VERSION_ID

# 防止 sourcemap 文件
tar -czf archive.tar.gz --exclude='*.map' -C ${dist_path} .
chown -R 1003 archive.tar.gz

# 创建目录
ssh -o StrictHostKeyChecking=no -p 65022 deploy_html@119.91.221.218 "cd /usr/share/nginx/html/ci-build/; mkdir -p ${project}/${env}/;"

rsync -e "ssh -o StrictHostKeyChecking=no -p 65022" -av \
  archive.tar.gz \
  deploy_html@119.91.221.218:"/usr/share/nginx/html/ci-build/${project}/${env}/${ROLLBACK_VERSION_ID}.tar.gz"
if [[ $? -gt 0 ]]; then
  echo "rsync error"
  exit 1
fi
ssh -o StrictHostKeyChecking=no -p 65022 deploy_html@119.91.221.218 "cd /usr/share/nginx/html/ci-build/${project}/${env}/; rm latest.tar.gz; cp ${ROLLBACK_VERSION_ID}.tar.gz latest.tar.gz"

# ims的map位置不在dist目录下
if [ $project = "ims" ]; then
  dist_path='./dist/js-react-agency'
fi

# 上传 sourcemap
# $CI_SHELL_PATH/scripts/upload-sourcemap.sh $dist_path

if [ $project = "hera" ] \
 || [ $project = "contact-book" ] \
 || [ $project = "panel" ] \
 || [ $project = "user-profile" ]; then
  $CI_SHELL_PATH/scripts/diff.sh $dist_path $project $env
fi

# 生成git_status.txt
$CI_SHELL_PATH/scripts/git-status.sh $project $env

# 企业微信通知
$CI_SHELL_PATH/scripts/wecom-notify.sh $project $env